
#ifndef _MEMBER_BTABLEWIDGET_H_
#define _MEMBER_BTABLEWIDGET_H_

#include <BFocusEvent>
#include <BPainter>
#include <BFont>
#include <BTable>
#include <BLabel>
#include <BLineEdit>
#include <BTableItem>
#include <BSizePolicy>
#include <BTableWidget>
#include "member_BTableItem.h"

#define table_member(table)		((member_BTableWidget*)((BTableWidget*)table)->_ptr)

namespace BWE
{
	class ItemEdit : public BLineEdit
	{
	public:
		ItemEdit();
		virtual void showEvent(const BEvent& event);
		virtual void focusEvent(const BFocusEvent& focusEvent);
		BTableItem*		item;
	};

	class HeaderSection
	{
	public:
		HeaderSection()
		{
			visible = true;
			dirty = true;
			sortMode = 0;
			align = Align_Center;
			size = 0;
			sizeHint = 0;
			policy = Policy_Preferred;
		}

		void adjust()
		{
			textRect = rect.align(align, textSize);
			BRect arrowSpace(textRect.right(), rect.top(), rect.right() - textRect.right(), rect.height());
			BSize arrowSize(textSize.height() * 0.6, textSize.height() * 0.6);
			sortRect = arrowSpace.align(Align_Center, arrowSize);
		}

		bool		visible;
		char		sortMode;
		bool		dirty;
		BString		text;
		BString		tr_text;
		Align		align;
		int			size;
		BSize		sizeHint;
		BRect		rect;
		BSize		textSize;
		BRect		textRect;
		BRect		sortRect;
		Policy		policy;
	};
	class TableHeader
	{
	public:
		TableHeader();
		~TableHeader();

		void updateDirtySections(const BStyle* style);
		int sectionUnderPoint(const BPoint& point);
		int handleUnderPoint(const BPoint& point);
		BRect handleRect(int index);

		bool drag(const BPoint& pos);
		void paint(BPainter& painter);

		bool			dirty;
		Orientation		orientation;
		int				space;
		bool			sortable;
		bool			resizable;

		int				hoverIndex;
		bool			pressed;
		int				handle;
		bool			draging;
		BPoint			dragingPoint;

		BSize			hintSize;
		int				show_first;
		int				show_count;
		BArray<HeaderSection>	sections;
	};

	class member_BTableWidget
	{
	public:
		member_BTableWidget(BTableWidget* boss);
		~member_BTableWidget();

		BTableItem* itemAtPoint(const BPoint& pos);

		void adjustHeader();
		void adjustItemRects();
		void freshColumnRects(int column);
		bool sortColumn(int column);

	public:
		BTableWidget*		boss;
		GridStyle			gridStyle;
		BMargins			margins;
		SelectMode			selectMode;
		BHolder<ItemEdit>	itemEdit;

		bool				dirty;
		bool				pressed;
		bool				ctrl;
		bool				shift;
		BTableItem*			hoverItem;
		BTableItem*			shiftItem;

		TableHeader			rowHeader;
		TableHeader			columnHeader;
		BArray<BTableItem*>			selectedItems;
		BTable<BTableItemHolder>	items;
	};
}

#endif